﻿using System.Globalization;
using System.IO;

namespace ThingsGateway.Foundation
{
    public enum DataTimeSaveMode
    {
        /// <summary>
        /// 按分钟间隔
        /// </summary>
        ByEveryMinute = 1,

        /// <summary>
        /// 按小时间隔
        /// </summary>
        ByEveryHour = 2,

        /// <summary>
        /// 按天间隔
        /// </summary>
        ByEveryDay = 3,

        /// <summary>
        /// 按周间隔
        /// </summary>
        ByEveryWeek = 4,

        /// <summary>
        /// 按月间隔
        /// </summary>
        ByEveryMonth = 5,

        /// <summary>
        /// 按季度间隔
        /// </summary>
        ByEverySeason = 6,

        /// <summary>
        /// 按年间隔
        /// </summary>
        ByEveryYear = 7,
    }
    /// <summary>
    /// 根据时间来区分不同的文件存储日志组件<br />
    /// </summary>
    public class LogNetDateTime : LogNetFileBase
    {

        #region Private Fields
        public DataTimeSaveMode DataTimeSaveMode { get; set; }

        #endregion Private Fields

        #region Public Constructors

        /// <summary>
        /// 根据时间存储的日志组件<br />
        /// </summary>
        /// <param name="filePath">存储路径</param>
        /// <param name="dataTimeSaveMode">文件间隔</param>
        /// <param name="fileQuantity">日志文件数量上限</param>
        public LogNetDateTime(string filePath, DataTimeSaveMode dataTimeSaveMode = DataTimeSaveMode.ByEveryYear, int fileQuantity = -1)
        {
            FilePath = filePath;
            DataTimeSaveMode = dataTimeSaveMode;
            FileQuantity = fileQuantity;

            if (string.IsNullOrEmpty(filePath) || Directory.Exists(filePath))
            {
                return;
            }
            Directory.CreateDirectory(filePath);
        }

        #endregion Public Constructors

        #region Public Methods

        public override string GetFileSaveName()
        {
            if (string.IsNullOrEmpty(FilePath))
            {
                return string.Empty;
            }
            switch (DataTimeSaveMode)
            {
                case DataTimeSaveMode.ByEveryMinute:
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".txt");

                case DataTimeSaveMode.ByEveryHour:
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd_HH") + ".txt");

                case DataTimeSaveMode.ByEveryDay:
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");

                case DataTimeSaveMode.ByEveryWeek:
                    int weekOfYear = new GregorianCalendar().GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.Year.ToString() + "_W" + weekOfYear.ToString() + ".txt");

                case DataTimeSaveMode.ByEveryMonth:
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.ToString("yyyy_MM") + ".txt");

                case DataTimeSaveMode.ByEverySeason:
                    string filePath = FilePath;
                    string[] strArray = new string[5]
                    {
            "Logs_",
            null,
            null,
            null,
            null
                    };
                    DateTime now = DateTime.Now;
                    strArray[1] = now.Year.ToString();
                    strArray[2] = "_Q";
                    now = DateTime.Now;
                    strArray[3] = ((now.Month / 3) + 1).ToString();
                    strArray[4] = ".txt";
                    string path2 = string.Concat(strArray);
                    return Path.Combine(filePath, path2);

                case DataTimeSaveMode.ByEveryYear:
                    return Path.Combine(FilePath, "Logs_" + DateTime.Now.Year.ToString() + ".txt");

                default:
                    return string.Empty;
            }
        }

        public override string ToString()
        {
            return string.Format(nameof(LogNetDateTime) + "[{0}]", DataTimeSaveMode);
        }

        #endregion Public Methods

    }
}